import {Request, Response, NextFunction, Router} from "express";
import {Repository} from "sequelize-typescript";
import {validationResult, body} from 'express-validator'
import {Comment} from "../models";
import {checkToken} from "../middlewares/jwt";

interface BodyRequestBody extends Request {
    body: {
        [key: string]: string | undefined
    }
}

const CommentRouterFactory = (
    commentRepository: Repository<Comment>,
    ) => Router()
        .post('/create', checkToken, [
                body('user_id').isLength({min: 1}),
                body('dy_id').isLength({min: 1}),
                body('user_name').isLength({min: 1}),
                body('user_picture').isLength({min: 1}),
                body('content').isLength({min: 1})
            ], async (req: BodyRequestBody, res: Response, next: NextFunction) => {
                const error = validationResult(req)
                if (!error.isEmpty()) {
                    return res.status(422).json({
                        errCode: 10000,
                        msg: error.array()
                    })
                }
                let {user_id, dy_id, user_name, user_picture, content} = req.body
                try {
                    const data = await commentRepository.create({
                        dy_id: dy_id,
                        user_id: user_id,
                        user_name: user_name,
                        user_picture: user_picture,
                        content: content,
                        created_at: new Date().getTime()
                    })
                    if (data) {
                        return res.json({
                            code: 1,
                            msg: '评论成功'
                        })
                    }


                } catch (e) {
                    res.json({
                        errCode: 10001,
                        msg: '操作失败'
                    })
                    next()
                }
            }
        )
        .post('/del', checkToken, [
                body('id').isLength({min: 1}),
            ], async (req: BodyRequestBody, res: Response, next: NextFunction) => {
                const error = validationResult(req)
                if (!error.isEmpty()) {
                    return res.status(422).json({
                        errCode: 10000,
                        msg: error.array()
                    })
                }
                let {id} = req.body

                try {
                    if (!id) {
                        return res.json({
                            errCode: 10001,
                            msg: '操作失败'
                        })
                    }
                    const list = await commentRepository.destroy({
                        where: {
                            id: id
                        }
                    })
                    if(list) {
                        return res.json({
                            code: 1,
                            msg: '删除评论成功'
                        })
                    }
                    return res.json({
                        errCode: 10001,
                        msg: '操作失败'
                    })
                } catch (e) {
                    res.json({
                        errCode: 10001,
                        msg: '操作失败'
                    })
                    next()
                }
            }
        )
// .get('/', async (req: BodyRequestBody, res: Response, next: NextFunction) => {
//         const list = await homeRepository.findAll()
//         res.json(list || {
//             errCode: 10001,
//             msg: '数据获取失败'
//         })
//     }
//     // homeRepository.findAll()
//     // .then(posts => res.json(posts))
//     // .catch(next)
// )

// .get('/', async (req, res, next) => {
//         const list = await homeRepository.findAll()
//         res.json(list || {
//             errCode: 10001,
//             msg: '数据获取失败'
//         })
//     }
//     // homeRepository.findByPk(req.params.id)
//     // .then(post => post
//     //   ? res.json(post)
//     //   : next({statusCode: 404}))
//     // .catch(next)
// )
//
// .post('/posts', (req, res, next) =>
//     homeRepository.create(req.body)
//         .then(post => res.json(post))
//         .catch(next)
// )

;
export default CommentRouterFactory
